package com.hhkj.admin.common.validator;

import cn.hutool.core.util.ObjectUtil;
import lombok.Setter;

import javax.validation.ConstraintValidatorContext;

@Setter
public abstract class AbstractValidatorChain<T> {

    private AbstractValidatorChain next;

    private boolean validate(T bean, ConstraintValidatorContext constraintValidatorContext) {
        return this.doValidate(bean, constraintValidatorContext, this);
    }

    protected abstract boolean doValidate(T bean, ConstraintValidatorContext constraintValidatorContext, AbstractValidatorChain validatorChain);

    public boolean validateNext(T bean, ConstraintValidatorContext constraintValidatorContext) {
        if (ObjectUtil.isEmpty(next)) {
            return true;
        } else {
            return next.validate(bean, constraintValidatorContext);
        }
    }
}
